package com.detech.sboh.base.log.util;

import com.detech.sboh.base.enumtype.LogTypeEnum;
import com.detech.sboh.base.enumtype.ModuleEnum;
import com.detech.sboh.base.hibernate.log.entity.TbLogSys;
import com.detech.sboh.base.log.service.LogService;
import com.detech.sboh.base.util.SpringContextUtils;
import com.detech.sboh.base.util.UserFun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
* @description 可在项目中任意位置记录日志的操作类
* @author 方安伦
* @date 2022/4/15 8:54
 **/
public class MyLOG {

    /**
    * @description 日志记录（完全版）
    * @author 方安伦
    * @date 2022/4/14 17:30
     **/
    public static void log(LogTypeEnum logType, ModuleEnum model, String user, String content, String methodUrl, String methodParams, String ip, Exception e, boolean saveDB) {
        TbLogSys sysLogEntity = new TbLogSys();
        sysLogEntity.setOperator(user);
        sysLogEntity.setContent(content);
        sysLogEntity.setModule(model.name());
        sysLogEntity.setMethodUrl(methodUrl);
        sysLogEntity.setMethodParams(methodParams);
        sysLogEntity.setIp(ip);
        sysLogEntity.setLogTime(new Date());
        sysLogEntity.setId(UserFun.getUUID32());
        if(e!=null){
            //有异常发生时，日志级别固定为ERROR
            sysLogEntity.setLogType(LogTypeEnum.ERROR.name());
        }else{
            sysLogEntity.setLogType(logType.name());
        }
        if(e!=null){
            //根据异常对象，取得异常堆栈信息（取前面200行）
            String stackTrace = UserFun.getStackTraceForHtml(e, 200);
            sysLogEntity.setStackTrace(stackTrace);
        }
        //日志输出到控制台和文件
        Logger log = LoggerFactory.getLogger("root");
        String showContent = user + " " + content + " " + methodParams;
        if("DEBUG".equals(logType.name())){
            log.debug(showContent);
        }else if("INFO".equals(logType.name())){
            log.info(showContent);
        }else if("WARN".equals(logType.name())){
            log.warn(showContent);
        }else if("ERROR".equals(logType.name())){
            log.error(showContent,e);
        }
        //保存系统日志到数据库
        if (saveDB || e != null) {
            LogService logService = SpringContextUtils.getBean(LogService.class);
            logService.saveSysLog(sysLogEntity);
        }
    }

    /**
    * @description 日志记录（简化版）
    * @author 方安伦
    * @date 2022/4/14 17:31
     **/
    public static void log(LogTypeEnum logType, ModuleEnum model, String user, String content, boolean saveDB) {
        TbLogSys sysLogEntity = new TbLogSys();
        sysLogEntity.setOperator(user);
        sysLogEntity.setContent(content);
        sysLogEntity.setModule(model.name());
        sysLogEntity.setLogTime(new Date());
        sysLogEntity.setId(UserFun.getUUID32());
        sysLogEntity.setLogType(logType.name());
        //日志输出到控制台和文件
        Logger log = LoggerFactory.getLogger("root");
        String showContent = user + " " + content;
        if("DEBUG".equals(logType.name())){
            log.debug(showContent);
        }else if("INFO".equals(logType.name())){
            log.info(showContent);
        }else if("WARN".equals(logType.name())){
            log.warn(showContent);
        }else if("ERROR".equals(logType.name())){
            log.error(showContent);
        }
        //保存系统日志到数据库
        if (saveDB) {
            LogService logService = SpringContextUtils.getBean(LogService.class);
            logService.saveSysLog(sysLogEntity);
        }
    }
}
